﻿

using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IotClientService.Models.Hbc;
public class PlcReadWord
{
    private PlcMemoryType m_MemoryType;

    private int m_StartAddr;

    private PlcDataType m_DataType;

    private int m_ReadCount;

    private byte[] m_CurReadValue;

    private byte[] m_OldReadValue;

    private bool m_CurReadComplete;

    private bool m_OldReadComplete;

    private bool m_CurConnection;

    private bool m_OldConnection;

    private int m_DBBlock;

    public PlcMemoryType MemoryType
    {
        get
        {
            return m_MemoryType;
        }
        set
        {
            m_MemoryType = value;
        }
    }

    public int StartAddr
    {
        get
        {
            return m_StartAddr;
        }
        set
        {
            m_StartAddr = value;
        }
    }

    public PlcDataType DataType
    {
        get
        {
            return m_DataType;
        }
        set
        {
            m_DataType = value;
        }
    }

    public int ReadCount
    {
        get
        {
            return m_ReadCount;
        }
        set
        {
            m_ReadCount = value;
        }
    }

    public byte[] CurReadValue
    {
        get
        {
            return m_CurReadValue;
        }
        set
        {
            m_CurReadValue = value;
        }
    }

    public byte[] OldReadValue
    {
        get
        {
            return m_OldReadValue;
        }
        set
        {
            m_OldReadValue = value;
        }
    }

    public bool CurReadComplete
    {
        get
        {
            return m_CurReadComplete;
        }
        set
        {
            m_CurReadComplete = value;
        }
    }

    public bool OldReadComplete
    {
        get
        {
            return m_OldReadComplete;
        }
        set
        {
            m_OldReadComplete = value;
        }
    }

    public bool CurConnection
    {
        get
        {
            return m_CurConnection;
        }
        set
        {
            m_CurConnection = value;
        }
    }

    public bool OldConnection
    {
        get
        {
            return m_OldConnection;
        }
        set
        {
            m_OldConnection = value;
        }
    }

    public int DBBlock
    {
        get
        {
            return m_DBBlock;
        }
        set
        {
            m_DBBlock = value;
        }
    }

    private PlcReadWord()
    {
    }

    public PlcReadWord(PlcMemoryType _MemoryType, int _StartAddr, PlcDataType _DataType, int _ReadWordCount, int _DBBlock = 0)
    {
        m_MemoryType = _MemoryType;
        m_StartAddr = _StartAddr;
        m_DataType = _DataType;
        m_ReadCount = _ReadWordCount;
        m_DBBlock = _DBBlock;
        CurReadValue = new byte[_ReadWordCount * 2];
        OldReadValue = new byte[_ReadWordCount * 2];
    }

    public bool IsNeedReport()
    {
        if (IsReadCompleteChanged() || IsConnectionChanged() || IsValueChanged())
        {
            return true;
        }

        return false;
    }

    private bool IsReadCompleteChanged()
    {
        if (CurReadComplete != OldReadComplete)
        {
            return true;
        }

        return false;
    }

    private bool IsConnectionChanged()
    {
        if (CurConnection != OldConnection)
        {
            return true;
        }

        return false;
    }

    private bool IsValueChanged()
    {
        for (int i = 0; i < CurReadValue.Length; i++)
        {
            if (CurReadValue[i] != OldReadValue[i])
            {
                return true;
            }
        }

        return false;
    }

    public void UpdateSetTo(PlcReadWord setTarget)
    {
        setTarget.OldReadComplete = m_OldReadComplete;
        setTarget.CurReadComplete = m_CurReadComplete;
        setTarget.OldConnection = m_OldConnection;
        setTarget.CurConnection = m_CurConnection;
        Array.Copy(CurReadValue, 0, setTarget.CurReadValue, 0, CurReadValue.Length);
        Array.Copy(OldReadValue, 0, setTarget.OldReadValue, 0, OldReadValue.Length);
    }
}
